package com.shugl.study.leetcode;


import java.util.ArrayList;
import java.util.List;

/**
 * Description 求两个正序集合合并后的中位数
 *
 * @author Shugl
 * @version 1.0
 * @since 2021/6/18 10:29
 */
public class GetMedian {


    public static void main(String[] args) {
        int[] nums1 = {1};
        int[] nums2 = {};
        System.out.println(findMedianSortedArrays(nums1, nums2));
    }


    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int totalLength = nums1.length + nums2.length;
        boolean isDouble = totalLength % 2 == 0;
        List<Integer> newList = new ArrayList<>();
        for (int i : nums1) {
            newList.add(i);
        }
        for (int i : nums2) {
            newList.add(i);
        }
        newList.sort(Integer::compareTo);
        if (isDouble) {
            if (totalLength == 1) {
                return newList.get(0);
            } else {
                return (newList.get(totalLength / 2) + newList.get(totalLength / 2 - 1)) / 2D;
            }
        } else {
            return newList.get(totalLength / 2);
        }
    }
}
